/**
 * 通用js方法封装处理
 * 依赖xxx
 * Copyright (c) 2020 zengzhijun
 */
// 查询 Permission 下拉框
// function showPermissionSelectBox($eleObj, formObj, $){
//     $eleObj.append('<option value="" selected>--- 请选择权限 ---</option>')
//     $.each(JSON.parse(sessionStorage.getItem("permission-select-box")),function(index, obj){
//         //console.log(obj.permissionId)
//         $eleObj.append('<option value="'+ obj.permissionId +'" disabled>'+ obj.permissionName +'</option>')
//         const childs = obj.children
//         if(childs != null && childs.length > 0){
//             $.each(childs, function (i, c) {
//                 $eleObj.append('<option value="'+ c.permissionId +'">'+ '&nbsp;&nbsp;&nbsp;&nbsp;|----&nbsp;' +  c.permissionName +'</option>')
//             })
//         }
//     });
//     formObj.render('select','permission')
// }
// // 查询 Menu 下拉框
// function showMenuSelectBox($eleObj, formObj, $) {
//     const list = JSON.parse(sessionStorage.getItem("menu-select-box"))
//     //console.log(list)
//     $eleObj.append('<option value="0" selected>顶级目录</option>')
//     doShowMenuSelectBox(list, "", $eleObj, $)
//     formObj.render('select','permission')
// }
// function doShowMenuSelectBox(list, prefix, $eleObj, $) {
//     $.each(list, function (index, obj) {
//         $eleObj.append('<option value="'+ obj.menuId +'">' + prefix + obj.title +'</option>')
//         const childs = obj.children
//         if(childs.length > 0){
//             doShowMenuSelectBox(childs, '&nbsp;&nbsp;&nbsp;&nbsp;|----&nbsp;', $eleObj, $)
//         }
//     })
// }



function runDatetimePicker(){
    $("#search-datetime-start").datetimepicker({
        language:  'zh-cn',
        format: 'yyyy-mm-dd',
        minView: "month",
        todayBtn:  1,
        autoclose: 1,
        endDate : new Date(),
    }).on('changeDate', function(event) {
        event.preventDefault();
        event.stopPropagation();
        var startTime = event.date;
        $('#search-datetime-end').datetimepicker('setStartDate',startTime);
    });
    $("#search-datetime-end").datetimepicker({
        language:  'zh-cn',
        format: 'yyyy-mm-dd',
        minView: "month",
        todayBtn:  1,
        autoclose: 1,
        endDate : new Date(),
    }).on('changeDate', function(event) {
        event.preventDefault();
        event.stopPropagation();
        var endTime = event.date;
        $("#search-datetime-start").datetimepicker('setEndDate',endTime);
    });
}

// 提取URL中参数为对象
function parseQueryString(url){
    var str = url.split('?')[1];
    var result = {};
    var temp = (str || '').split('&');
    for(var i=0; i<temp.length; i++){
        var temp2 = (temp[i] || '').split('=');
        result[temp2[0]] = temp2[1];
    }
    return result;
}


// 需要引入 layui.js
var doOpenCreate = function(t, url) {
    layui.use(['layer'],function(){
        var layer = layui.layer;
        var form = layui.form;
        layer.open({
            type: 2,
            title: t,
            area: ['800px', '480px'],
            //maxmin: true,
            content: url
        });
        form.render();
    });
};

// 删除对象
// url: /invoice/ajax/deleteByIds?ids=
// ids: new Array()
//layer: layui layer obj
//tabins: layui table obj
var doDeleteByIds = function(url, ids, layer, tabins){
    //var url = '/invoice/ajax/deleteByIds?ids=' + ids;
    if(ids.length == 0){
        layer.msg("操作失误！");
        return;
    }
    layer.confirm('真的删除数据吗？(此操作不可逆)', function(index){
        $.ajax({
            url:url + ids,
            type:'post',
            dataType:'json',
            traditional:true,
            success:function(data, status){
                var d = data.code;
                if(d == RESP_SUCCESS) {
                    layer.msg(data.name);
                    tabins.reload();//重载
                }else{
                    layer.msg(data.name, {icon: 5, timeout:2},function () {
                        return false;//不跳转页面
                    })
                }
            }
        });
    });
}
var doUpdateById = function (url, jsonObj) {
    layer.confirm('真的修改数据吗？(此操作不可逆)', function(index){
        $.ajax({
            url:url,
            type:'post',
            dataType:'json',
            data: jsonObj,
            traditional:true,
            success:function(data, status){
                var d = data.code;
                if(d == RESP_SUCCESS) {
                    layer.msg(data.name);
                    tabins.reload();//重载
                }else{
                    layer.msg(data.name, {icon: 5, timeout:2},function () {
                        return false;//不跳转页面
                    })
                }
            }
        });
    });
}

// 获取到 菜单 的 data-id 的值
function getSiderMenuDataId() {
    const dataid = parent.$('.Hui-admin-aside-wrapper li.current').find('a').data('id');
    return dataid;
}